package cn.iocoder.an.module.business.service.filedaily;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.an.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.an.framework.tenant.core.aop.TenantIgnore;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.*;
import cn.iocoder.an.module.business.controller.admin.filedaily.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.filedaily.FileDailyDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import cn.iocoder.an.module.business.dal.mysql.filedaily.FileDailyMapper;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.an.module.business.enums.ErrorCodeConstants.*;

/**
 * 报 Service 实现类
 *
 * @author zzy
 */
@Service
@Validated
public class FileDailyServiceImpl implements FileDailyService {

    @Resource
    private FileDailyMapper fileDailyMapper;

    @Override
    public String createFileDaily(FileDailySaveReqVO createReqVO) {
        // 插入
        FileDailyDO fileDaily = BeanUtils.toBean(createReqVO, FileDailyDO.class);
        fileDailyMapper.insert(fileDaily);

        // 返回
        return fileDaily.getDevID();
    }

    @Override
    public void updateFileDaily(FileDailySaveReqVO updateReqVO) {
        // 校验存在
        validateFileDailyExists(updateReqVO.getDevID());
        // 更新
        FileDailyDO updateObj = BeanUtils.toBean(updateReqVO, FileDailyDO.class);
        fileDailyMapper.updateById(updateObj);
    }

    @Override
    public void deleteFileDaily(String id) {
        // 校验存在
        validateFileDailyExists(id);
        // 删除
        fileDailyMapper.deleteById(id);
    }

    @Override
        public void deleteFileDailyListByIds(List<LocalDate> ids) {
        // 删除
        fileDailyMapper.deleteByIds(ids);
        }


    private void validateFileDailyExists(String id) {
        if (fileDailyMapper.selectById(id) == null) {
            throw exception(FILE_DAILY_NOT_EXISTS);
        }
    }

    @Override
    public FileDailyDO getFileDaily(String id) {
        return fileDailyMapper.selectById(id);
    }

    @Override
    public PageResult<FileDailyDO> getFileDailyPage(FileDailyPageReqVO pageReqVO) {
        return fileDailyMapper.selectPage(pageReqVO);
    }

    @DS("report")
    @Override
    public List<FileDailyDO> getList() {
        LambdaQueryWrapperX<FileDailyDO> queryWrapperX = new LambdaQueryWrapperX<>();
        LocalDate targetDate = LocalDate.now().minusDays(1);
// 如果 itime 是 LocalDate 类型
        queryWrapperX.eq(FileDailyDO::getItime, targetDate);

// 如果 itime 是 LocalDateTime 类型
        queryWrapperX.between(
                FileDailyDO::getItime,
                targetDate.atStartOfDay(),
                targetDate.atTime(LocalTime.MAX)
        );
        List<FileDailyDO> fileDailyDOS = fileDailyMapper.selectList(queryWrapperX);
        System.out.println("fileDailyDOS:" + fileDailyDOS);
        return fileDailyDOS;
    }

}